<template>
  <el-container class="dashboard-container">
    <el-aside width="200px">
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :router="true"
        :collapse="isCollapse"
      >
        <el-menu-item index="/dashboard/profile">
          <el-icon><User /></el-icon>
          <template #title>个人信息</template>
        </el-menu-item>

        <!-- 系统管理 -->
        <el-sub-menu
          index="system-management"
          v-if="userStore.isAdmin"
        >
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>系统管理</span>
          </template>
          <el-menu-item index="/dashboard/users">
            <el-icon><Avatar /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/role-permissions">
            <el-icon><Key /></el-icon>
            <template #title>角色权限</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/operation-logs">
            <el-icon><Document /></el-icon>
            <template #title>操作日志</template>
          </el-menu-item>
        </el-sub-menu>

        <!-- 基础数据 -->
        <el-sub-menu
          index="basic-data"
          v-if="userStore.isAdmin || userStore.isTeacher || userStore.isGraduate"
        >
          <template #title>
            <el-icon><DataBoard /></el-icon>
            <span>基础数据</span>
          </template>
          <el-menu-item index="/dashboard/majors">
            <el-icon><Reading /></el-icon>
            <template #title>专业管理</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/courses">
            <el-icon><Reading /></el-icon>
            <template #title>课程管理</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/course-scores">
            <el-icon><Tickets /></el-icon>
            <template #title>课程得分</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/graduation-requirements">
            <el-icon><TrophyBase /></el-icon>
            <template #title>毕业要求</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/indicators">
            <el-icon><Flag /></el-icon>
            <template #title>指标点</template>
          </el-menu-item>
        </el-sub-menu>

        <!-- 评价管理 -->
        <el-sub-menu
          index="evaluation-management"
          v-if="userStore.isAdmin || userStore.isTeacher || userStore.isGraduate"
        >
          <template #title>
            <el-icon><TrendCharts /></el-icon>
            <span>评价管理</span>
          </template>
          <el-menu-item index="/dashboard/course-support-matrices">
            <el-icon><Grid /></el-icon>
            <template #title>课程支撑矩阵</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/course-goal-achievements">
            <el-icon><EditPen /></el-icon>
            <template #title>课程目标达成度</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/graduation-requirement-achievements">
            <el-icon><Finished /></el-icon>
            <template #title>毕业要求达成度</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/evaluation-mechanisms">
            <el-icon><ScaleToOriginal /></el-icon>
            <template #title>评价机制</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/teacher-evaluations">
            <el-icon><Notebook /></el-icon>
            <template #title>教师评价</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/graduate-self-evaluations">
            <el-icon><DocumentChecked /></el-icon>
            <template #title>毕业生自评</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/employer-evaluations">
            <el-icon><House /></el-icon>
            <template #title>用人单位评价</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/internship-unit-evaluations">
            <el-icon><School /></el-icon>
            <template #title>实习单位评价</template>
          </el-menu-item>
          <el-menu-item
            index="/dashboard/direct-evaluations"
            v-if="userStore.isAdmin || userStore.isTeacher || userStore.isGraduate"
          >
            <el-icon><EditPen /></el-icon>
            <template #title>直接评价</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/graduate-requirement-achievement" v-if="userStore.isAdmin || userStore.isTeacher || userStore.isGraduate">
            <el-icon><DataAnalysis /></el-icon>
            <template #title>毕业生毕业要求达成度</template>
          </el-menu-item>
          <el-menu-item index="/dashboard/graduation-requirement-achievement-report" v-if="userStore.isAdmin || userStore.isTeacher || userStore.isGraduate">
            <el-icon><DataLine /></el-icon>
            <template #title>毕业要求达成度报表</template>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-left">
          <el-button
            @click="toggleCollapse"
          >
            <el-icon>
              <component :is="isCollapse ? 'Expand' : 'Fold'" />
            </el-icon>
          </el-button>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              {{ userStore.userInfo.username }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../store'
import { ElMessageBox } from 'element-plus'
import {
  User,
  House,
  School,
  Document,
  Expand,
  Fold,
  ArrowDown,
  Setting,
  Avatar,
  Key,
  DataBoard,
  Reading,
  Tickets,
  TrophyBase,
  Flag,
  TrendCharts,
  Grid,
  EditPen,
  Finished,
  ScaleToOriginal,
  Notebook,
  DocumentChecked,
  DataAnalysis,
  DataLine
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const isCollapse = ref(false)

const activeMenu = computed(() => route.path)

onMounted(() => {
  console.log('Dashboard.vue mounted. userStore.userRole:', userStore.userRole)
  console.log('userStore.isAdmin:', userStore.isAdmin)
  console.log('userStore.isTeacher:', userStore.isTeacher)
  console.log('userStore.isGraduate:', userStore.isGraduate)
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/dashboard/profile')
  } else if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      userStore.logout()
      router.push('/login')
    } catch {
      // 用户取消操作
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  height: 100vh;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}

.el-menu {
  border-right: none;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-dropdown .el-icon {
  margin-left: 5px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 